<template>
  <div id="userManagePage">
    <!-- 搜索表单 -->
    <a-form layout="inline" :model="searchParams" @finish="doSearch">
      <a-form-item label="账号">
        <a-input v-model:value="searchParams.userAccount" placeholder="输入账号" />
      </a-form-item>
      <a-form-item label="用户名">
        <a-input v-model:value="searchParams.userName" placeholder="输入用户名" />
      </a-form-item>
      <a-form-item>
        <a-button type="primary" html-type="submit">搜索</a-button>
      </a-form-item>
    </a-form>
    <a-divider />
    <!-- 表格 -->
    <a-table
      :columns="columns"
      :data-source="data"
      :pagination="pagination"
      @change="doTableChange"
    >
      <template #bodyCell="{ column, record }">
        <template v-if="column.dataIndex === 'userAvatar'">
          <a-image :src="record.userAvatar" :width="120" />
        </template>
        <template v-else-if="column.dataIndex === 'userRole'">
          <div v-if="record.userRole === 'admin'">
            <a-tag color="green">管理员</a-tag>
          </div>
          <div v-else>
            <a-tag color="blue">普通用户</a-tag>
          </div>
        </template>
        <template v-else-if="column.dataIndex === 'createTime'">
          {{ dayjs(record.createTime).format('YYYY-MM-DD HH:mm:ss') }}
        </template>
        <template v-else-if="column.key === 'action'">
          <a-button class="delete-btn" @click="doDelete(record.id)">删除</a-button>
        </template>
      </template>
    </a-table>
  </div>
</template>
<script lang="ts" setup>
import { computed, onMounted, reactive, ref } from 'vue'
import { deleteUser, listUserVoByPage } from '@/api/userController.ts'
import { message } from 'ant-design-vue'
import dayjs from 'dayjs'

const columns = [
  {
    title: 'id',
    dataIndex: 'id',
  },
  {
    title: '账号',
    dataIndex: 'userAccount',
  },
  {
    title: '用户名',
    dataIndex: 'userName',
  },
  {
    title: '头像',
    dataIndex: 'userAvatar',
  },
  {
    title: '简介',
    dataIndex: 'userProfile',
  },
  {
    title: '用户角色',
    dataIndex: 'userRole',
  },
  {
    title: '创建时间',
    dataIndex: 'createTime',
  },
  {
    title: '操作',
    key: 'action',
  },
]

// 展示的数据
const data = ref<API.UserVO[]>([])
const total = ref(0)

// 搜索条件
const searchParams = reactive<API.UserQueryRequest>({
  pageNum: 1,
  pageSize: 10,
})

// 获取数据
const fetchData = async () => {
  const res = await listUserVoByPage({
    ...searchParams,
  })
  if (res.data.data) {
    data.value = res.data.data.records ?? []
    total.value = res.data.data.totalRow ?? 0
  } else {
    message.error('获取数据失败，' + res.data.message)
  }
}

// 分页参数
const pagination = computed(() => {
  return {
    current: searchParams.pageNum ?? 1,
    pageSize: searchParams.pageSize ?? 10,
    total: total.value,
    showSizeChanger: true,
    showTotal: (total: number) => `共 ${total} 条`,
  }
})

// 表格分页变化时的操作
const doTableChange = (page: { current: number; pageSize: number }) => {
  searchParams.pageNum = page.current
  searchParams.pageSize = page.pageSize
  fetchData()
}

// 搜索数据
const doSearch = () => {
  // 重置页码
  searchParams.pageNum = 1
  fetchData()
}

// 删除数据
const doDelete = async (id: string) => {
  if (!id) {
    return
  }
  const res = await deleteUser({ id })
  if (res.data.code === 0) {
    message.success('删除成功')
    // 刷新数据
    fetchData()
  } else {
    message.error('删除失败')
  }
}

// 页面加载时请求一次
onMounted(() => {
  fetchData()
})
</script>

<style scoped>
#userManagePage {
  min-height: 100vh;
  padding: 24px;
  background:
    linear-gradient(180deg, #0a0e27 0%, #1a1f3a 8%, #1e2338 20%, #0f1419 100%),
    radial-gradient(circle at 20% 80%, rgba(59, 130, 246, 0.25) 0%, transparent 50%),
    radial-gradient(circle at 80% 20%, rgba(139, 92, 246, 0.22) 0%, transparent 50%),
    radial-gradient(circle at 40% 40%, rgba(16, 185, 129, 0.18) 0%, transparent 50%);
  position: relative;
  overflow: hidden;
}

/* 科技感网格背景 */
#userManagePage::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-image:
    linear-gradient(rgba(59, 130, 246, 0.15) 1px, transparent 1px),
    linear-gradient(90deg, rgba(59, 130, 246, 0.15) 1px, transparent 1px),
    linear-gradient(rgba(139, 92, 246, 0.12) 1px, transparent 1px),
    linear-gradient(90deg, rgba(139, 92, 246, 0.12) 1px, transparent 1px);
  background-size:
    100px 100px,
    100px 100px,
    20px 20px,
    20px 20px;
  pointer-events: none;
  animation: gridFloat 20s ease-in-out infinite;
  opacity: 0.6;
}

@keyframes gridFloat {
  0%,
  100% {
    transform: translate(0, 0);
  }
  50% {
    transform: translate(5px, 5px);
  }
}

#userManagePage > * {
  position: relative;
  z-index: 2;
}

#userManagePage :deep(.ant-divider) {
  border-color: rgba(59, 130, 246, 0.3);
}

/* 表单样式 - 深色主题 */
#userManagePage :deep(.ant-form-item-label > label) {
  color: rgba(255, 255, 255, 0.9);
}

#userManagePage :deep(.ant-input) {
  background: rgba(20, 25, 40, 0.8);
  border-color: rgba(59, 130, 246, 0.3);
  color: rgba(255, 255, 255, 0.9);
}

#userManagePage :deep(.ant-input:hover) {
  border-color: rgba(59, 130, 246, 0.5);
  background: rgba(20, 25, 40, 0.9);
}

#userManagePage :deep(.ant-input:focus),
#userManagePage :deep(.ant-input-focused) {
  border-color: rgba(59, 130, 246, 0.8);
  background: rgba(20, 25, 40, 0.9);
  box-shadow: 0 0 0 2px rgba(59, 130, 246, 0.2);
}

#userManagePage :deep(.ant-input::placeholder) {
  color: rgba(255, 255, 255, 0.4);
}

/* 表格样式 - 深色主题 */
#userManagePage :deep(.ant-table) {
  background: transparent;
  color: rgba(255, 255, 255, 0.9);
}

#userManagePage :deep(.ant-table-thead > tr > th) {
  background: rgba(20, 25, 40, 0.8);
  border-color: rgba(59, 130, 246, 0.3);
  color: rgba(255, 255, 255, 0.95);
  font-weight: 600;
}

#userManagePage :deep(.ant-table-tbody > tr > td) {
  background: rgba(15, 20, 35, 0.6);
  border-color: rgba(59, 130, 246, 0.2);
  color: rgba(255, 255, 255, 0.9);
}

#userManagePage :deep(.ant-table-tbody > tr:hover > td) {
  background: rgba(30, 40, 60, 0.8) !important;
  border-color: rgba(59, 130, 246, 0.4) !important;
}

#userManagePage :deep(.ant-table-tbody > tr) {
  transition: background-color 0.2s, border-color 0.2s;
}

#userManagePage :deep(.ant-table-tbody > tr:not(:hover) > td) {
  background: rgba(15, 20, 35, 0.6) !important;
}

#userManagePage :deep(.ant-table-tbody > tr.ant-table-row-selected > td) {
  background: rgba(30, 50, 80, 0.8) !important;
}

#userManagePage :deep(.ant-table-tbody > tr.ant-table-row-selected:hover > td) {
  background: rgba(40, 60, 100, 0.9) !important;
}

/* 分页器样式 - 深色主题 */
#userManagePage :deep(.ant-pagination) {
  color: rgba(255, 255, 255, 0.9);
}

#userManagePage :deep(.ant-pagination-item) {
  background: rgba(20, 25, 40, 0.8);
  border-color: rgba(59, 130, 246, 0.3);
}

#userManagePage :deep(.ant-pagination-item a) {
  color: rgba(255, 255, 255, 0.8);
}

#userManagePage :deep(.ant-pagination-item:hover) {
  border-color: rgba(59, 130, 246, 0.6);
  background: rgba(30, 40, 60, 0.9);
}

#userManagePage :deep(.ant-pagination-item:hover a) {
  color: rgba(255, 255, 255, 0.95);
}

#userManagePage :deep(.ant-pagination-item-active) {
  background: rgba(59, 130, 246, 0.8);
  border-color: rgba(59, 130, 246, 0.8);
}

#userManagePage :deep(.ant-pagination-item-active a) {
  color: #fff;
}

#userManagePage :deep(.ant-pagination-prev),
#userManagePage :deep(.ant-pagination-next) {
  background: rgba(20, 25, 40, 0.8);
  border-color: rgba(59, 130, 246, 0.3);
}

#userManagePage :deep(.ant-pagination-prev:hover),
#userManagePage :deep(.ant-pagination-next:hover) {
  border-color: rgba(59, 130, 246, 0.6);
  background: rgba(30, 40, 60, 0.9);
}

#userManagePage :deep(.ant-pagination-prev .ant-pagination-item-link),
#userManagePage :deep(.ant-pagination-next .ant-pagination-item-link) {
  color: rgba(255, 255, 255, 0.8);
  background: transparent;
}

#userManagePage :deep(.ant-pagination-options) {
  color: rgba(255, 255, 255, 0.9);
}

#userManagePage :deep(.ant-pagination-options-size-changer .ant-select-selector) {
  background: rgba(20, 25, 40, 0.8) !important;
  border-color: rgba(59, 130, 246, 0.3) !important;
  color: rgba(255, 255, 255, 0.9) !important;
}

#userManagePage :deep(.ant-pagination-options-size-changer .ant-select-selector:hover) {
  border-color: rgba(59, 130, 246, 0.6) !important;
  background: rgba(30, 40, 60, 0.9) !important;
}

#userManagePage :deep(.ant-pagination-total-text) {
  color: rgba(255, 255, 255, 0.8);
}

/* 表格滚动条样式 */
#userManagePage :deep(.ant-table-body)::-webkit-scrollbar {
  width: 8px;
  height: 8px;
}

#userManagePage :deep(.ant-table-body)::-webkit-scrollbar-track {
  background: rgba(20, 25, 40, 0.5);
  border-radius: 4px;
}

#userManagePage :deep(.ant-table-body)::-webkit-scrollbar-thumb {
  background: rgba(59, 130, 246, 0.5);
  border-radius: 4px;
}

#userManagePage :deep(.ant-table-body)::-webkit-scrollbar-thumb:hover {
  background: rgba(59, 130, 246, 0.7);
}

/* 确保表格容器背景透明 */
#userManagePage :deep(.ant-table-container) {
  background: transparent;
}

#userManagePage :deep(.ant-table-content) {
  background: transparent;
}

/* 表格空状态 */
#userManagePage :deep(.ant-empty) {
  color: rgba(255, 255, 255, 0.6);
}

#userManagePage :deep(.ant-empty-description) {
  color: rgba(255, 255, 255, 0.6);
}

/* 删除按钮样式 - 红色渐变按钮 */
#userManagePage :deep(.delete-btn) {
  background: linear-gradient(135deg, #ff4757 0%, #ee5a6f 50%, #c44569 100%) !important;
  border: none !important;
  color: #ffffff !important;
  border-radius: 6px !important;
  padding: 4px 16px !important;
  height: auto !important;
  font-weight: 500 !important;
  box-shadow: 0 2px 8px rgba(255, 71, 87, 0.3), 
              0 1px 3px rgba(0, 0, 0, 0.2),
              inset 0 1px 0 rgba(255, 255, 255, 0.2) !important;
  transition: all 0.3s ease !important;
  position: relative !important;
  overflow: hidden !important;
}

#userManagePage :deep(.delete-btn::before) {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 30%;
  background: linear-gradient(180deg, rgba(255, 255, 255, 0.3) 0%, transparent 100%);
  pointer-events: none;
}

#userManagePage :deep(.delete-btn:hover) {
  background: linear-gradient(135deg, #ff6b7a 0%, #ff6b81 50%, #d3546f 100%) !important;
  box-shadow: 0 4px 12px rgba(255, 71, 87, 0.4), 
              0 2px 6px rgba(0, 0, 0, 0.3),
              inset 0 1px 0 rgba(255, 255, 255, 0.25) !important;
  transform: translateY(-1px) !important;
}

#userManagePage :deep(.delete-btn:active) {
  background: linear-gradient(135deg, #ee5a6f 0%, #c44569 50%, #a8325a 100%) !important;
  box-shadow: 0 1px 4px rgba(255, 71, 87, 0.3), 
              0 0 2px rgba(0, 0, 0, 0.2),
              inset 0 2px 4px rgba(0, 0, 0, 0.2) !important;
  transform: translateY(0) !important;
}

#userManagePage :deep(.delete-btn:focus) {
  outline: none !important;
  box-shadow: 0 2px 8px rgba(255, 71, 87, 0.3), 
              0 1px 3px rgba(0, 0, 0, 0.2),
              inset 0 1px 0 rgba(255, 255, 255, 0.2),
              0 0 0 2px rgba(255, 71, 87, 0.3) !important;
}
</style>
